<template>
	<view class="login-page">
		<!-- #ifndef H5 -->
		<u-navbar placeholder leftIconSize="0" bgColor="transparent"></u-navbar>
		<!-- #endif -->

		<view class="top-box">
			<view class="label">欢迎登录</view>
		</view>

		<view class="container">
			<view class="card-box">
				<u--form labelPosition="left" labelWidth="20rpx" :model="form" :rules="rules" ref="uForm">
					<u-form-item prop="mobile">
						<u--input v-model="form.mobile" border="none" placeholder="请输入手机号"></u--input>
					</u-form-item>
					<u-form-item prop="loginpass">
						<u-input v-model="form.loginpass" border="none" :password="!showPassword" placeholder="请输入登陆密码">
							<template #suffix>
								<view @click="showPassword = !showPassword">
									<u-icon :name="showPassword ? 'eye-off' : 'eye-fill'" color="#666" size="36rpx"></u-icon>
								</view>
							</template>
						</u-input>
					</u-form-item>
				</u--form>

				<button class="btn" @click="loginSubmit">立即登录</button>
			</view>
		</view>
	</view>
</template>

<script>
	import { login, getConfig } from '@/api/api'

	export default {
		data () {
			return {
				logo: '',
				
				form: {
					mobile: '',
					loginpass: '',
					type: 1
				},
				rules: {
					mobile: [
						{required: true, message: '请输入 11 位手机号', trigger: 'blur'},
						{ validator: (rule, value, callback) => { return uni.$u.test.mobile(value) },
							message: '手机号码不正确',
							trigger: 'blur',
						}
					],
					loginpass: { required: true, message: '请输入密码', trigger: 'blur' },
				},
				showPassword: false,
			}
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules)
		},
		onLoad () {
			
		},
		methods: {
			loginSubmit () {
				this.$refs.uForm.validate().then(valid => {
					if (valid) {
						this.loginReq(this.form)
					}
				}).catch(errors => {
					console.log('loginSubmit errors');
				})
			},
			loginReq (form) {
				uni.showLoading({ mask: true })

				login(form).then(res => {
					uni.hideLoading({ noConflict: true })
					if (res.data) {
						uni.showToast({ title: res.msg, icon: 'none', mask: true })
						uni.setStorageSync('token', res.data.token.access_token)
						setTimeout(() => {
							uni.switchTab({
								url: '/pages/index/index'
							})
						}, 200)
					}
				})
			},
		}
	}
</script>

<style lang="scss">
.login-page {
	position: relative;
	min-height: 100%;
	background-color: $bg-color;
	.page-top {
		width: 750rpx;
		position: absolute;
		top: 0;
		left: 0;
		z-index: 0;
		filter: blur(20px);
		.bg {
			width: 750rpx;
		}
		.mask {
			position: absolute;
			bottom: 0;
			left: 0;
			right: 0;
			height: 60rpx;
			background-image: linear-gradient(0deg, #f1f7ff, rgba($color: #f1f7ff, $alpha: 0));
		}
	}
	.top-box {
		height: 332rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		position: relative;
		z-index: 4;
		.image {
			width: 134rpx;
			height: 134rpx;
			border-radius: 50%;
		}
		.label {
			margin-top: 40rpx;
			color: #333;
			font-size: 32rpx;
			font-weight: bold;
		}
	}
	.container {
		padding: 0 32rpx 32rpx;
		position: relative;
		z-index: 4;
	}
	.card-box {
		padding: 52rpx 42rpx;
		border-radius: 20rpx;
		background-color: #fff;
		::v-deep {
			.u-form-item {
				&:not(:last-child) {
					margin-bottom: 24rpx;
				}
				.u-form-item__body {
					padding: 20rpx;
					border-radius: 72rpx;
					background-color: #f7f7f7;
				}
				.u-form-item__body__right__message {
					margin-top: 8rpx;
				}
			}
		}
		.btn {
			margin-top: 28rpx;
			border-radius: 88rpx;
			height: 88rpx;
			color: #fff;
			font-size: 32rpx;
			line-height: 88rpx;
			background-color: $primary-color;
			box-shadow: 0 0 24rpx rgba($color: $primary-color, $alpha: 0.2);
			&[disabled] {
				background-color: rgba($color: $primary-color, $alpha: 0.4);
				box-shadow: none;
			}
		}
		.btn-top {
			margin-top: 52rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			.register {
				display: flex;
				align-items: center;
				.text {
					color: $primary-color;
					font-size: 28rpx;
				}
			}
			.forgot {
				color: #666;
				font-size: 28rpx;
			}
		}
		.no-login {
			margin-top: 20rpx;
			height: 88rpx;
			color: #666;
			font-size: 32rpx;
			line-height: 88rpx;
			background-color: transparent;
		}
	}
	.protocol-radio {
		position: fixed;
		left: 32rpx;
		right: 32rpx;
		bottom: 80rpx;
		z-index: 6;
		display: flex;
		justify-content: center;
	}
}
</style>
